package com.module.ble.hDu
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.Looper
import android.os.RemoteException
import android.util.Log
import com.sxr.sdk.ble.keepfit.aidl.IRemoteService

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2025/3/24
 *
 * 描述：
 *
 * 修订历史：
 *
 */
class HDActionReceiver(private val mService: IRemoteService?) : BroadcastReceiver() {

    private val TAG = HDActionReceiver::class.simpleName

    private fun tryBondDevice(bd: BluetoothDevice, leftTimes: Int) {
        Handler(Looper.getMainLooper()).postDelayed({
            val bResult: Boolean = HDSysUtils.boundDeviceAPI(bd)
            Log.i(TAG, "create bond device $leftTimes : $bResult")
            if (leftTimes > 0) {
                val times = leftTimes - 1
                tryBondDevice(bd, times)
            }
        }, 100)
    }

    override fun onReceive(context: Context, intent: Intent) {
        val action = intent.action
        if (action.equals(HDBleService.ECG_SWITCH, ignoreCase = true)) {
            val state = intent.getBooleanExtra("state", false)
            val mode = intent.getIntExtra("mode", 0)
            try {
                mService?.setEcgMode(state, mode) ?: kotlin.run {
                    Log.i(TAG, "service null cannot set ecg mode")
                }
            } catch (e: RemoteException) {
                e.printStackTrace()
            }
        } else if (action.equals(HDBleService.FACTORYMODE_TEST_SWITCH, ignoreCase = true)) {
            val mode = intent.getBooleanExtra("mode", false)
            try {
                mService?.startFactoryTestMode(mode) ?: kotlin.run {
                    Log.i(TAG, "service null cannot set start factory mode")
                }
            } catch (e: RemoteException) {
                e.printStackTrace()
            }
        } else if (action.equals(
                HDCommonAttributes.ACTION_NOTIFY_CLASSIC_BT_NEED_CONNECT,
                ignoreCase = true
            )
        ) {
//            Handler().postDelayed({
//                val alertDialog: AlertDialog = AlertDialog.Builder(
//                    this@MainActivity
//                )
//                    .setMessage(getString(R.string.msg_bt_pair_error) + mClassicBtDeviceBtName)
//                    .setPositiveButton(R.string.action_bt_setting,
//                        DialogInterface.OnClickListener { dialog, which ->
//                            val intent = Intent(Settings.ACTION_BLUETOOTH_SETTINGS)
//                            startActivity(intent)
//                        })
//                    .setNegativeButton(R.string.action_cancel, null).create()
//                alertDialog.show()
//            }, 1000)
        } else if (action.equals(HDCommonAttributes.ACTION_NOTIFY_CLASSIC_BT_RETRY_BOND, ignoreCase = true)) {
            // retry 10 times 100 ms delay
            val deviceMac = intent.getStringExtra("deviceMac")
            val bd = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(deviceMac)
            tryBondDevice(bd, HDCommonAttributes.MAX_RETRY_BOND_TIMES)
        } else if (action.equals(
                HDCommonAttributes.ACTION_NOTIFY_CLASSIC_BT_CREATE_BOND,
                ignoreCase = true
            )
        ) {
            val bdBond = intent.getParcelableExtra<BluetoothDevice>("device")
            val bResultBond: Boolean = HDSysUtils.boundDeviceAPI(bdBond)
            Log.i(TAG, "create bond device: $bResultBond")
        } else if (action.equals(
                HDCommonAttributes.ACTION_REQUEST_DIAL_JSON_CONTENT,
                ignoreCase = true
            )
        ) {
            val fw = intent.getStringExtra("fw") ?: ""
            try {
                Log.i(TAG, "request dial json content: $fw")
                mService?.getDialServerInfo(fw) ?: kotlin.run {
                    Log.i(TAG, "service null cannot get dial server info")
                }
            } catch (e: RemoteException) {
                e.printStackTrace()
            }
        }
    }
}